package vn.bus.bk.adapter;

import java.util.ArrayList;
import vn.bus.bk.Menu;
import vn.bus.bk.R;
import vn.bus.bk.ViewInfoBus;
import vn.bus.bk.object.BusInfo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListBusAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<BusInfo> myListBus;
    private Activity mContext;
    private SharedPreferences myPreference;
    private Editor edit;

    public ListBusAdapter(Context activity, ArrayList<BusInfo> array) {
	this.myListBus = array;
	mContext = (Activity) activity;
	myPreference = mContext.getSharedPreferences(Menu.FAVORITE, 0);
	inflater = (LayoutInflater) activity
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
	// TODO Auto-generated method stub
	return myListBus.size();
    }

    public Object getItem(int arg0) {
	// TODO Auto-generated method stub
	return arg0;
    }

    public long getItemId(int arg0) {
	// TODO Auto-generated method stub
	return arg0;
    }

    public void addItem(BusInfo b) {
	this.myListBus.add(b);
    }
    
    public void updateList(ArrayList<BusInfo> listBus){
	this.myListBus = listBus;
    }

    public void clear() {
	this.myListBus.clear();
    }

    public View getView(int position, View convertView, ViewGroup arg2) {
	// TODO Auto-generated method stub
	BusHolder busHolder = new BusHolder();
	if (convertView == null) {
	    convertView = inflater.inflate(R.layout.item_row_bus, null);
	    busHolder.name = (TextView) convertView
		    .findViewById(R.id.tv_name_bus);
	    busHolder.number = (TextView) convertView
		    .findViewById(R.id.tv_number_bus);
	    busHolder.favorite = (ImageView) convertView
		    .findViewById(R.id.img_favorite);
	    convertView.setTag(busHolder);
	}

	final BusInfo b = myListBus.get(position);
	if (b != null) {
	    final BusHolder busHolder1 = (BusHolder) convertView.getTag();
	    busHolder1.number.setText("Tuyến số "
		    + String.valueOf(b.getNumber()));
	    busHolder1.name.setText(b.getName());

	    if (myPreference.getBoolean(String.valueOf(b.getName()), false)) {
		busHolder1.favorite
			.setImageResource(R.drawable.remove_favorite);
	    } else {
		busHolder1.favorite.setImageResource(R.drawable.add_favorite);
	    }

	    // Set action listener for all item

	    busHolder1.favorite.setOnClickListener(new View.OnClickListener() {

		public void onClick(View arg0) {
		    // TODO Auto-generated method stub
		    edit = myPreference.edit();
		    if (myPreference.getBoolean(String.valueOf(b.getName()),
			    false)) {
			busHolder1.favorite
				.setImageResource(R.drawable.add_favorite);
			edit.putBoolean(String.valueOf(b.getName()), false);
		    } else {
			busHolder1.favorite
				.setImageResource(R.drawable.remove_favorite);
			edit.putBoolean(String.valueOf(b.getName()), true);
		    }
		    edit.commit();

		}

	    });

	}
	convertView.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		ViewInfoBus.myBus = b;
		mContext.startActivity(new Intent(mContext, ViewInfoBus.class));
	    }
	});

	return convertView;
    }

    

}

class BusHolder {
    TextView number, name;
    ImageView favorite;
}
